﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace MINERP.DAL.InnerFactory
{
    public class InnerFactoryDAL : DALBase
    {
        /// <summary>
        /// 内部工厂获取委外记录
        /// </summary>
        /// <param name="inFactoryId"></param>
        /// <returns></returns>
        public List<OutProcessRecord> GetOutRocord(int inFactoryId)
        {
            int Status = (int)OutProcessRecordStatus.Fetched;
            int Status2 = (int)OutProcessRecordStatus2.Complete;
            int OrderStatus = (int)OrderInfoStatus.Doing;

            return DataEntites.OutProcessRecord.Where(p => p.orderInfo.status == OrderStatus && p.OutProcessShop.OutShopId == inFactoryId && p.Status != Status && p.Status2 != (int)Status2).ToList();

        }
        /// <summary>
        /// 根据条码取得记录
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public OutProcessRecord GetOutRecordByBarcode(string inBarCode, int inShopId)
        {
            return DataEntites.OutProcessRecord.Where(p => p.orderInfo.OrderProductCode == inBarCode && p.OutProcessShop.OutShopId == inShopId).FirstOrDefault();
        }
        /// <summary>
        /// 根据条码取得记录
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public OutProcessRecord GetOutRecordByBarcode(string inBarCode)
        {
            return DataEntites.OutProcessRecord.Where(p => p.orderInfo.OrderProductCode == inBarCode).FirstOrDefault();
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        public void SaveData()
        {
            DataEntites.SaveChanges();
        }
        /// <summary>
        /// 根据Id取得记录
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public OutProcessRecord GetOutRecordById(int inId)
        {
            return DataEntites.OutProcessRecord.Where(p => p.OutProcessRecordId == inId).FirstOrDefault();
        }
        public OutProcessShop GetShopByID(int inOutShopId)
        {
            return DataEntites.OutProcessShop.Where(p => p.OutShopId == inOutShopId).FirstOrDefault();
        }
        /// <summary>
        /// 取得所有的加工厂
        /// </summary>
        /// <returns></returns>
        public List<OutProcessShop> GetAllInnerFactory()
        {
            return DataEntites.OutProcessShop.ToList();
        }
        /// <summary>
        /// 获取打印信息
        /// </summary>
        /// <param name="inproductId"></param>
        /// <returns></returns>
        public DataTable GetPrintInfo(string inproductId)
        {

            Database db = null;

            DataSet dsPrint = null;
            try
            {
                db = DatabaseFactory.CreateDatabase();
                string sqlCommand = "select OrderProductName,CtmName,OrderProductCode,shopName,sum(photoinfo.totalNum) PageNum,FetchTime from orderInfo,Tb_OrderBag,Shop,photoinfo where orderInfo.OrderBagId=Tb_OrderBag.OrderBagId and  Tb_OrderBag.shopID=Shop.ShopId and orderInfo.ParentProductId=0  and orderInfo.orderproductid =photoInfo.orderproductid and  orderInfo.OrderProductCode='" + inproductId + "' group by OrderProductName,CtmName,OrderProductCode,shopName,PageNum,FetchTime";
                DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
                dsPrint = db.ExecuteDataSet(dbCommand);

            }
            catch
            {

            }
            if (dsPrint == null)
            {
                return null;
            }
            else
            {
                return dsPrint.Tables[0];
            }
        }
        public DataTable CreateFactoryInfo()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("ProductBarCode", typeof(string));
            dataTable.Columns.Add("ProductName", typeof(string));
            dataTable.Columns.Add("ShopName", typeof(string));
            dataTable.Columns.Add("CtmName", typeof(string));
            return dataTable;
        }
    }
}
